#include "MasterComponent.h"
#include "MainPanel.h"
#include "NutritionGuidePanel.h"
#include "ListPanels/MealListPanel.h"
#include "ListPanels/GroceryListPanel/GroceryListPanel.h"
#include "ListPanels/FoodListPanel.h"
#include "ListPanels/CalendarPanel.h"
#include "UtilitiesPanel.h"

MasterComponent::MasterComponent(int w, int h) :
	width(w), height(h)
{
	setPanelDimensions();
	PersistentFoodList::instance();
	DailyIntake::instance();

	add(mainPanel = new MainPanel(this, mdim.width, mdim.height), mdim);
	add(nutritionPanel = new NutritionGuidePanel(this, ndim.width, ndim.height), ndim);
	add(mealListPanel = new MealListPanel(this, mldim.width, mldim.height), mldim);
	add(foodListPanel = new FoodListPanel(this, fldim.width, fldim.height), fldim);
	add(groceryListPanel = new GroceryListPanel(this, gldim.width, gldim.height), gldim);
	add(utilitiesPanel = new UtilitiesPanel(this, udim.width, udim.height), udim);
	add(calendarPanel = new CalendarPanel(this, cdim.width, cdim.height), cdim);
	calendarPanel->setGroceryListView(groceryListPanel->getView());
	mainPanel->setVisible(true);

}

void MasterComponent::setPanelDimensions()
{
	mdim = Dimensions((int)(height * 0.8), (int)(height * 0.8));
	ndim = Dimensions((int)(height * 0.9), (int)(height * 0.9));
	mldim = Dimensions((int)(width * 0.7), (int)(height * 0.9));
	fldim = Dimensions((int)(width * 0.7), (int)(height * 0.9));
	gldim = Dimensions((int)(width * 0.7), (int)(height * 0.9));
	udim = Dimensions((int)(height * 0.8), (int)(height * 0.9));
	cdim = Dimensions((int)(width * 0.7), (int)(height * 0.9));
}

void MasterComponent::add(Component* component, Dimensions& dim)
{
	addChildComponent(component);
	component->setBounds(width / 2 - dim.width / 2, height / 2 - dim.height / 2, dim.width, dim.height);
}

void MasterComponent::paint(Graphics& g)
{
	g.fillAll(Colour(245, 245, 245));
//	g.fillAll(Colour(30, 30, 30));
}

void MasterComponent::switchPanels(int panelA, int panelB)
{
	setPanelVisible(panelA, false);
	setPanelVisible(panelB, true);
}

void MasterComponent::setPanelVisible(int panel, bool isVisible)
{
	switch(panel)
	{
		case MAIN:
			mainPanel->setVisible(isVisible);
			break;
		case NUTRITION:
			nutritionPanel->setVisible(isVisible);
			break;
		case MEAL_LIST:
			mealListPanel->setVisible(isVisible);
			break;
		case FOOD_LIST:
			foodListPanel->setVisible(isVisible);
			break;
		case GROCERY_LIST:
			groceryListPanel->setVisible(isVisible);
			break;
		case UTILITIES:
			utilitiesPanel->setVisible(isVisible);
			break;
		case CALENDAR:
			calendarPanel->setVisible(isVisible);
			break;
	}
}

void MasterComponent::resized(int newWidth, int newHeight)
{
	width = newWidth;
	height = newHeight;

	setPanelDimensions();

	mainPanel->setSize(mdim.width, mdim.height);
	nutritionPanel->setSize(ndim.width, ndim.height);
	mealListPanel->setSize(mldim.width, mldim.height);
	foodListPanel->setSize(fldim.width, fldim.height);
	groceryListPanel->setSize(gldim.width, gldim.height);
	utilitiesPanel->setSize(udim.width, udim.height);
	calendarPanel->setSize(cdim.width, cdim.height);
}
